package org.dawn.deveco.plugin.resourceeditor.utils;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CommonUtil {
    private static class DebounceActionListener implements ActionListener {
        private final JComponent component;
        private final ActionListener listener;

        public DebounceActionListener(JComponent component, ActionListener listener) {
            this.component = component;
            this.listener = listener;
        }

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            try {
                this.component.setEnabled(false);
                this.listener.actionPerformed(actionEvent);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                this.component.setEnabled(true);
            }
        }
    }

    public static ActionListener debounce(JComponent component, ActionListener listener) {
        return new DebounceActionListener(component, listener);
    }

//    public static ActionListener debounce(JComponent component, Action action) {
//        return new DebounceActionListener(component, action);
//    }
}
